import Vue from 'vue'
import VueRouter from 'vue-router'
import Index from '../view/Index'
import Login from '../view/Login'
import Register from '../view/Register'
import NotFound from '../view/Error404'
import Home from '../view/Home'
import InfoShow from '../view/InfoShow'
import FundList from '../view/FundList'

Vue.use(VueRouter)

const routes = [
  {path: '/',redirect: '/index',},
  {path:'/index',
    name:'index',
    component:Index,
    children:[
      {path:'',component:Home},
      {path:'/infoshow',name:'infoshow',component:InfoShow},
      {path:'/fundlist',name:'fundlist',component:FundList},
    ]
  },
  {path:'/login',name:'login',component:Login,},
  {path:'/register',name:'register',component:Register,},
  {path:'/*',name:'404',component:NotFound,}
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})
//路由守卫
router.beforeEach((to, from, next) => {
  //判断token是否存在，存在则表示在登录状态下
  const isLogin=localStorage.eleToken?true:false
  if(to.path=='/login'||to.path=='/register'){
    //登录和注册页面不论是否有token都可以正常访问
    next()
  }else{
    isLogin?next():next('/login')
  }
})
export default router
